package com.zw.yzk.kabu.view.activity.imagedetails;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.data.utils.LogUtils;
import com.zw.yzk.kabu.domain.interactor.DefaultOnNext;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.utils.PhoneUtils;
import com.zw.yzk.kabu.utils.QNImageUtils;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import butterknife.OnClick;
import cn.finalteam.galleryfinal.widget.zoonview.PhotoViewAttacher;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * Created by wei on 2017/3/30.
 */

public class OriginalImageListDetailsActivity extends ImageListDetailsActivity {

    private List<Integer> dList;

    private TextView download;

    public static void startOriginalImageListDetailsActivity(Context from, ArrayList<String> picUrl, String fromUrl, Rect fromRect) {
        Intent intent = new Intent(from, OriginalImageListDetailsActivity.class);
        intent.putExtra("url", picUrl);
        intent.putExtra("fromUrl", fromUrl);
        intent.putExtra("fromRect", fromRect);
        from.startActivity(intent);
        //禁用过渡动画
        if (from instanceof Activity) {
            ((Activity) from).overridePendingTransition(0, 0);
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_image_original_list;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        dList = new ArrayList<>();
        download = (TextView) findViewById(R.id.view_original);
        download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                downloadOriginalImage();
            }
        });

        mIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                boolean show = true;
                for (int i = 0; i < dList.size(); i++) {
                    if (dList.get(i) == position) {
                        show = false;
                    }
                }
                download.setVisibility(show ? View.VISIBLE : View.GONE);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public void downloadOriginalImage() {
        LogUtils.e("zhanwei", "downloadOriginalImage");
        Observable.create(new Observable.OnSubscribe<File>() {
            @Override
            public void call(Subscriber<? super File> subscriber) {
                OriginalImageListDetailsActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        LoadingDialogManager.getInstance().showLoadingDialog(OriginalImageListDetailsActivity.this);
                    }
                });
                try {
                    File file = Glide.with(OriginalImageListDetailsActivity.this)
                            .load(adapter.getCurrentUrl())
                            .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                            .get();
                    subscriber.onNext(file);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultOnNext<File>() {
                    @Override
                    public void call(File file) {
                        dList.add(adapter.getCurrentPosition());
                        Glide.with(OriginalImageListDetailsActivity.this)
                                .load(file)
                                .into(new SimpleTarget<GlideDrawable>() {
                            @Override
                            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                                if (resource instanceof GlideBitmapDrawable) {
                                    adapter.getPrimaryItem().setImageBitmap(((GlideBitmapDrawable) resource).getBitmap());
                                    PhotoViewAttacher attacher = new PhotoViewAttacher(adapter.getPrimaryItem());
                                    attacher.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
                                        @Override
                                        public void onPhotoTap(View view, float x, float y) {
                                            if (adapter.listener != null) {
                                                adapter.listener.onClicked(view);
                                            }
                                        }
                                    });
                                }
                            }
                        });

                        LoadingDialogManager.getInstance().dismissLoadingDialog();
                        download.setVisibility(View.GONE);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        LoadingDialogManager.getInstance().dismissLoadingDialog();
                    }
                });
    }

//    public void downloadOriginalImage() {
//        LogUtils.e("zhanwei", "downloadOriginalImage");
//        Observable.create(new Observable.OnSubscribe<Bitmap>() {
//            @Override
//            public void call(Subscriber<? super Bitmap> subscriber) {
//                OriginalImageListDetailsActivity.this.runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        LoadingDialogManager.getInstance().showLoadingDialog(OriginalImageListDetailsActivity.this);
//                    }
//                });
//                try {
//                    File file = Glide.with(OriginalImageListDetailsActivity.this)
//                            .load(adapter.getCurrentUrl())
//                            .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
//                            .get();
//                    Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(file));
//                    subscriber.onNext(bitmap);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                    throw new RuntimeException(e);
//                }
//            }
//        }).subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new DefaultOnNext<Bitmap>() {
//                    @Override
//                    public void call(Bitmap bitmap) {
//                        dList.add(adapter.getCurrentPosition());
//                        int width = PhoneUtils.getScreenWidth(OriginalImageListDetailsActivity.this);
//                        float scaleX = width / (float) bitmap.getWidth();
//
//                        adapter.getPrimaryItem().setImageBitmap(bitmap);
//                        PhotoViewAttacher attacher = new PhotoViewAttacher(adapter.getPrimaryItem());
//                        attacher.setScale(scaleX);
//                        attacher.setRotationTo(0);
//                        attacher.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
//                            @Override
//                            public void onPhotoTap(View view, float x, float y) {
//                                if (adapter.listener != null) {
//                                    adapter.listener.onClicked(view);
//                                }
//                            }
//                        });
//
//                        LoadingDialogManager.getInstance().dismissLoadingDialog();
//                        download.setVisibility(View.GONE);
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        super.onError(e);
//                        LoadingDialogManager.getInstance().dismissLoadingDialog();
//                    }
//                });
//    }
}
